# -*- coding: utf-8 -*-
"""
Created on Wed Nov  9 11:01:04 2016

@author: admin
"""


#we have make a simple change!!!!!!!!!!

#anything funny ?????


from multiprocessing import Queue
import time
import signal
from sdk import *
import sys

import os
import pandas  as pd
import numpy as np
from pandas import Series, DataFrame
import glob
import re
import time

# 循环处理各个月份
for i in range(1, 2):
    #进行月份的转化
    s = str(i).zfill(2)
    #选取每一年的时间
    time_of_this_year = '2016'
    time_of_this_date = 20160203
    s1 = 'DZH-SZ' + time_of_this_year + s + '-TXT'
    all_date_list = glob.glob(r'E:\股票\股票l1\股票5档分笔%s年TXT\%s\*' % (time_of_this_year,s1))  # 获取一个月里面所有的天数列表

    sdk = SDK('https://api.kuaiyudian.com/rpc')  # 获取前一日数据
    #定义数据表
    preClosePriceDic = {}
    #对每一天的数据循环进行处理
    for each_day_list in all_date_list:
        try:
            current_date = re.search(r'\d{8}$', each_day_list).group()  # 截取每一个地址的日期
            current_date_font_change = time.strftime('%Y-%m-%d', time.strptime(current_date, '%Y%m%d'))  # 转换日期格式
        except:
            print('we can not get the right date' + current_date)
            continue
            #对特定日期进行处理
        if int(current_date) < time_of_this_date:
            print(current_date + 'is proceeding')
            data = sdk.get_bars_by_date(current_date_font_change)  # 获取收盘价
            for d in data:  # 循环data数据，截取昨日收盘价
                preClosePriceDic[d['symbol']] = d['preClosePrice']
            n2 = r'E:\股票\股票l1\股票5档分笔%s年TXT\%s\%s\*.txt' % (time_of_this_year,s1, current_date)  # 将时间导入到新的地址里面

            f = glob.glob(n2)  # 获取一天里面所有数据的列表
            #对一天内所有的数据进行处理
            for k in f:
                try:
                    # 由于数据列标题有时候后会出现改变，所以把日期和时间改为位置1和位置2
                    k2 = re.search(r'\d{6}[_]', k)  # 截取数据的名字
                    stockcode = (re.search(r'\d{6}', k2.group())).group()  # 股票名称
                    # print(stockcode + 'is proceding')
                    # print('this----->' + k + 'can not been cleaned')
                    stocksymbol = 'sz' + stockcode
                except:
                    print('we can not get this file' + k)
                    continue

                try:
                    if stocksymbol in preClosePriceDic.keys():
                        # 读取每一个txt格式数据到df
                        df = pd.read_csv(k, sep=',', encoding='gb2312', parse_dates={'timestamp': [0, 1]})
                        # print('we can read'+ stocksymbol)
                        df['preClosePrice'] = float(preClosePriceDic[stocksymbol])
                        df['timestamp'] = df['timestamp'].astype(np.int64) // 10 ** 9
                        if stocksymbol == 'sh603633':
                            df['hardenPrice'] = round(df['preClosePrice'] * 1.44, 3)  # 新股则乘以1.44
                        else:
                            df['hardenPrice'] = round(df['preClosePrice'] * 1.1, 3)  # 旧股则乘以1.1
                        #由于特殊值的出现，需要对数据进行容错化处理
                        if '额' in df.columns:
                            df.rename(columns={'额': 'amount'}, inplace=True)
                        elif '额、属性(持仓增减)' in df.columns:
                            df.rename(columns={'额、属性(持仓增减)': 'amount'}, inplace=True)
                        df['dehardenPrice'] = round(df['preClosePrice'] * 0.9, 3)
                        df['highPrice'] = df['成交价'].expanding().max()
                        df['lowPrice'] = df['成交价'].expanding().min()
                        df['openPrice'] = df.get_value(0, '成交价')  # 开盘价  如何获取当前日的第一个价格
                        df['symbol'] = stocksymbol  # 手动。。。。。如何取出名字
                        df['stockName'] = stockcode  # 手动。。。。如何将其进行一一对应
                        df['orderNum'] = 5  # 固定为5
                        df['status'] = 'PZ'  # 分比数据固定为PZ

                        df.rename(columns={
                            '成交价': 'nowPrice',
                            '总量': 'volume',
                            'B1价': 'bidv1',
                            'B1量': 'bidp1',
                            'B2价': 'bidv2',
                            'B2量': 'bidp2',
                            'B3价': 'bidv3',
                            'B3量': 'bidp3',
                            'B4价': 'bidv4',
                            'B4量': 'bidp4',
                            'B5价': 'bidv5',
                            'B5量': 'bidp5',
                            'S1价': 'askv1',
                            'S1量': 'askp1',
                            'S2价': 'askv2',
                            'S2量': 'askp2',
                            'S3价': 'askv3',
                            'S3量': 'askp3',
                            'S4价': 'askv4',
                            'S4量': 'askp4',
                            'S5价': 'askv5',
                            'S5量': 'askp5', }, inplace=True)
                        # 重新排序

                        df2 = df.reindex(columns=['symbol',
                                                  'stockName',
                                                  'timestamp',
                                                  'preClosePrice',
                                                  'hardenPrice',
                                                  'dehardenPrice',
                                                  'openPrice',
                                                  'highPrice',
                                                  'lowPrice',
                                                  'nowPrice',
                                                  'volume',
                                                  'amount',
                                                  # 'askVolume',
                                                  # 'bidVolume',
                                                  # 'totalTrades', #当日成交笔数
                                                  'orderNum',
                                                  'status',
                                                  'bidv1',
                                                  'bidp1',
                                                  'bidv2',
                                                  'bidp2',
                                                  'bidv3',
                                                  'bidp3',
                                                  'bidv4',
                                                  'bidp4',
                                                  'bidv5',
                                                  'bidp5',
                                                  'askv1',
                                                  'askp1',
                                                  'askv2',
                                                  'askp2',
                                                  'askv3',
                                                  'askp3',
                                                  'askv4',
                                                  'askp4',
                                                  'askv5',
                                                  'askp5',
                                                  ])
                        #生产新的文件
                        newpath = r'E:\stock_data_rx_daily-sparate\rx_data_sz_%s/%s/' % (time_of_this_year,current_date)
                        if not os.path.exists(newpath):
                            os.makedirs(newpath)
                        df2.to_csv(newpath + '/%s.csv' % (stockcode), index=False, header=False)

                except:
                    print('we can not change the right function of this' + k)
                    continue

            #rx_all_data_list = glob.glob(r'E:\stock_data_rx_daily-sparate\rx_data_sh_%s\*'%(time_of_this_year))


            print('we are emerging date with ' +current_date)
            outfile = open(r'E:\stock_data_rx\SZ\%s\%s.csv'%(time_of_this_year,current_date),'a')
            g = glob.glob(r'E:\stock_data_rx_daily-sparate\rx_data_sz_%s\%s\*.csv' % (time_of_this_year,current_date))
            for n in g:
                inputfile = open(n, 'r')
                # print 'we can open this file'  + n
                for line in inputfile:
                    outfile.write(line)
            outfile.close()
